#include "rotationchart.h"

#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QScrollBar>
#include <QDebug>

int RotationChart::ROLLING_WIN_W = 676;
int RotationChart::ROLLING_WIN_H = 398;

RotationChart::RotationChart(QWidget *parent) : QWidget(parent)
{
    m_imagePlayWidget = new QScrollArea(this);
    m_imageWidget = new QWidget();
    m_backImageBtn = new QPushButton(this);
    m_nextImageBtn = new QPushButton(this);
    m_backImageLab = new QLabel(this);
    m_nextImageLab = new QLabel(this);
    m_imagePlayWidget->setFrameShape(QFrame::NoFrame);

    m_imagePlayWidget->setProperty("barPos", 0);
    m_imagePlayWidget->setFixedSize(ROLLING_WIN_W, ROLLING_WIN_H);
    m_backImageBtn->setObjectName("back");
    m_nextImageBtn->setObjectName("next");
    m_backImageBtn->setIcon(QIcon(":/icon/backIcon.png"));
    m_nextImageBtn->setIcon(QIcon(":/icon/nextIcon.png"));
    m_backImageBtn->setFixedSize(14, 27);
    m_nextImageBtn->setFixedSize(14, 27);
    m_backImageBtn->setStyleSheet("QPushButton{border:none;background-color:transparent;border-radius:4px;}");
    m_nextImageBtn->setStyleSheet("QPushButton{border:none;background-color:transparent;border-radius:4px;}");

    m_imagePlayWidget->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); //设置水平滚动条
    m_imagePlayWidget->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);   //设置垂直滚动条
    m_imagePlayWidget->setWindowFlags(Qt::FramelessWindowHint);

    // 切换图片
    connect(m_backImageBtn, &QPushButton::clicked, this, &RotationChart::onClicked);
    connect(m_nextImageBtn, &QPushButton::clicked, this, &RotationChart::onClicked);

    QVBoxLayout *vLayout1 = new QVBoxLayout;
    vLayout1->addStretch();
    vLayout1->addWidget(m_backImageBtn);
    vLayout1->addStretch();
    vLayout1->setSpacing(0);
    vLayout1->setMargin(0);
    m_backImageLab->setLayout(vLayout1);
    QVBoxLayout *vLayout2 = new QVBoxLayout;
    vLayout2->addStretch();
    vLayout2->addWidget(m_nextImageBtn);
    vLayout2->addStretch();
    vLayout2->setSpacing(0);
    vLayout2->setMargin(0);
    m_nextImageLab->setLayout(vLayout2);

    QHBoxLayout *hLayout = new QHBoxLayout;
    hLayout->addStretch();
    hLayout->addWidget(m_backImageLab);
    hLayout->addSpacing(60);
    hLayout->addWidget(m_imagePlayWidget);
    hLayout->addSpacing(60);
    hLayout->addWidget(m_nextImageLab);
    hLayout->addStretch();
    hLayout->setSpacing(0);
    hLayout->setMargin(0);

    setLayout(hLayout);

    // 动画切换类;
    m_opacityAnimation = new QPropertyAnimation(m_imagePlayWidget, "barPos");
    // 这里要设置的动画时间
    m_opacityAnimation->setDuration(400);

    // 动画变化
    connect(m_opacityAnimation, SIGNAL(valueChanged(const QVariant &)), this,
            SLOT(slotImageIndexChange(const QVariant &)));
}

RotationChart::~RotationChart()
{
    if (m_opacityAnimation != nullptr) {
        delete m_opacityAnimation;
        m_opacityAnimation = nullptr;
    }
}

void RotationChart::setImageList(const QStringList &imageFileNameList)
{
    m_imageFileNameList = imageFileNameList;
    if (m_imageFileNameList.isEmpty()) {
        // 没有图片默认显示
        QLabel *defaultLab = new QLabel(m_imageWidget);
        defaultLab->setText(tr("No tutorial"));

        QHBoxLayout *hLayout = new QHBoxLayout;
        hLayout->addStretch();
        hLayout->addWidget(defaultLab);
        hLayout->addStretch();
        m_imageWidget->setLayout(hLayout);
        // 隐藏左右按钮
        m_backImageBtn->hide();
        m_nextImageBtn->hide();
    } else if (m_imageFileNameList.count() == 1) {
        // 设置放置图片的界面大小 w = 图片数量*滚动窗口大小
        m_imageWidget->setFixedSize(m_imageFileNameList.count() * ROLLING_WIN_W, ROLLING_WIN_H - 4);
        // 若是只有一张图片
        QLabel *oneLab = new QLabel(m_imageWidget);
        // 默认显示第一张图片
        QPixmap pixmap = QPixmap(m_imageFileNameList.first());
        // Lab自适应图片大小
        pixmap.scaled(oneLab->size(), Qt::KeepAspectRatio);
        oneLab->setScaledContents(true);
        oneLab->setPixmap(pixmap);

        QHBoxLayout *hLayout = new QHBoxLayout;
        hLayout->addWidget(oneLab);
        hLayout->setSpacing(0);
        hLayout->setMargin(0);
        m_imageWidget->setLayout(hLayout);
        // 隐藏左右按钮
        m_backImageBtn->hide();
        m_nextImageBtn->hide();
    } else if (m_imageFileNameList.count() > 1) {
        // 设置放置图片的界面大小 w = 图片数量*滚动窗口大小
        m_imageWidget->setFixedSize(m_imageFileNameList.count() * ROLLING_WIN_W, ROLLING_WIN_H - 4);
        // 多张图片;
        QHBoxLayout *hLayout = new QHBoxLayout;
        // 循环放置多张图片
        for (int i = 0; i < m_imageFileNameList.count(); i++) {
            QLabel *oneLab = new QLabel(m_imageWidget);
            QPixmap pixmap = QPixmap(m_imageFileNameList.value(i));
            pixmap.scaled(oneLab->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
            oneLab->setScaledContents(true);
            oneLab->setPixmap(pixmap);
            hLayout->addWidget(oneLab);
        }
        hLayout->setSpacing(0);
        hLayout->setMargin(0);
        m_imageWidget->setLayout(hLayout);
        // 显示第一张图片滑块处于初始位置
        m_currentDrawImageIndex = 0;

        m_backImageBtn->hide();
        m_nextImageBtn->show();
    }

    m_imagePlayWidget->setWidget(m_imageWidget);
}

void RotationChart::clearImageList()
{
    m_imageFileNameList.clear();
    delete m_imageWidget;
    m_imageWidget = nullptr;
    m_imageWidget = new QWidget();
}

void RotationChart::slotImageIndexChange(const QVariant &value)
{
    // 响应动画变化，移动滑块位置
    QScrollBar *bar = m_imagePlayWidget->horizontalScrollBar();
    bar->setSliderPosition(value.toInt());
}

void RotationChart::onClicked()
{
    QPushButton *btn = qobject_cast<QPushButton *>(sender());
    QString str = btn->objectName();

    if ("back" == str) {
        // 设置ImageOpacity属性值的变化范围;
        m_opacityAnimation->setStartValue(m_currentDrawImageIndex);
        m_opacityAnimation->setEndValue(m_currentDrawImageIndex - ROLLING_WIN_W);
        // 设置当前滑块位置
        m_currentDrawImageIndex -= ROLLING_WIN_W;
    } else {
        // 设置ImageOpacity属性值的变化范围;
        m_opacityAnimation->setStartValue(m_currentDrawImageIndex);
        m_opacityAnimation->setEndValue(m_currentDrawImageIndex + ROLLING_WIN_W);
        // 设置当前滑块位置
        m_currentDrawImageIndex += ROLLING_WIN_W;
    }
    // 开始动画
    m_opacityAnimation->start();

    if (0 == m_currentDrawImageIndex) {
        // 第一张图片
        m_backImageBtn->hide();
        m_nextImageBtn->show();
    } else if (ROLLING_WIN_W == m_currentDrawImageIndex) {
        // 第一张图片
        m_backImageBtn->show();
        m_nextImageBtn->hide();
    } else {
        m_backImageBtn->show();
        m_backImageBtn->show();
    }
}

void RotationChart::setLightUI()
{
    m_backImageBtn->setIcon(QIcon(":/icon/backIcon.png"));
    m_nextImageBtn->setIcon(QIcon(":/icon/nextIcon.png"));
}

void RotationChart::setDarkUI()
{
    m_backImageBtn->setIcon(QIcon(":/icon/backDarkIcon.png"));
    m_nextImageBtn->setIcon(QIcon(":/icon/nextDarkIcon.png"));
}
